// subPages/health/search/search.js
const app = getApp();
var util =  require('../../../utils/util.js');
var json =  require('../../../data/json.js');
var loadMoreView,p = 1,num = app.globalData.pageNum,type = 'goods'
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    util.setNavBarColor("#ffffff",app.globalData.themeColor);
    loadMoreView = that.selectComponent("#loadMoreView")
    if(options.type){
      that.setData({
        searchType: options.type
      })
    }
    if(app.globalData.communityId){
      that.setData({
        communityId: app.globalData.communityId
      })
    }else{
      app.communityIdCallback = communityId => {
        if (communityId != '') {
          this.setData({
            communityId: communityId
          })
        }
      }
    }
  },

  //searchInput输入
  searchInput:function(e){
    this.setData({
      searchTxt : e.detail.value,
      isSearch: true
    })
    if(this.data.searchTxt == ''){
      this.setData({
        isSearch: false
      })
    }
  },

  //删除按钮
  deleteBtn:function(){
    this.setData({
      searchTxt : '',
      isSearch: false
    })
  },

  //搜索按钮
  searchBtn:function(){
    var that = this
    if(that.data.searchTxt){
      if(that.data.searchType == 'goods'){
        that.getSearchShopData(that.data.searchTxt)
      }
    }else{
      util.showToast('搜索内容不能为空','none',1000)
    }
  },

  //获取搜索商品数据
  getSearchShopData:function(searchTxt){
    //获取搜索商品数据
    util.getAjax(app.globalData.apiUrl +'/get_community_health_shop_product_like',this.getShopData,{p: p,community_id: this.data.communityId,num: num,type: 1,like: searchTxt},this.shopDataErr)
  },

  getShopData(res){
    var that = this
    if(res.data.list.length != 0){
      var list = []
      if(p == 1) {
        list = res.data.list
      } else {
        list = that.data.goodsSearchList.concat(res.data.list)
      }
      that.setData({
        goodsSearchList: list
      })
    }else{
      that.setData({
        goodsSearchList: []
      })
    }
    loadMoreView.loadMoreComplete(res.data,type)
  },

  //获取商品数据异常
  shopDataErr(err){
    if(p>1){
      loadMoreView.loadMoreFail()
    }else{
      util.showModal('提示','获取商品失败，请向上滑动屏幕')
      this.setData({
        isFail: true
      })
    }
  },

  loadMoreListener: function(e) {
    p += 1
    that.getSearchShopData(that.data.searchTxt)
  },

  clickLoadMore: function(e) {
    that.getSearchShopData(that.data.searchTxt)
  },

  //点击购物车
  shopCartBtn:function(){
    wx.navigateTo({
      url: '/subPages/health/shopCart/shopCart'
    })
  },

  //点击食品
  clickGoods:function(e){
    if(app.globalData.userId){
      var id = e.currentTarget.dataset.id
      wx.navigateTo({
        url: '/subPages/health/healthShopDetails/healthShopDetails?id=' + id,
      })
    }else{
      util.showModelAll('提示','该功能需要授权登录后使用，请进行授权登录',function(){
        if (app.globalData.userInfo) {
          //已授权
          wx.redirectTo({
            url: '/pages/login/login'
          })
        }else{
          // 在没有 open-type=getUserInfo 版本的兼容处理
          if(app.globalData.isAuthorized){
            app.userInfoReadyCallback = res => {
              if (res) {
                //已授权
                wx.redirectTo({
                  url: '/pages/login/login'
                })
              }else{
                //未授权
                wx.navigateTo({
                  url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
                })
              }
            }
          }else{
            wx.navigateTo({
              url: '/pages/authorizedLogin/authorizedLogin?isLogin='+ false,
            })
          }
        }
      })
    }
  },

  //回到顶部
  goTop: function (e) {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  // 获取滚动条当前位置
  onPageScroll: function (e) {
    // console.log(e)
    if (e.scrollTop > 600) {
      this.setData({
        isTop: true
      });
    } else {
      this.setData({
        isTop: false
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if(that.data.isFail){
      that.getSearchShopData(that.data.searchTxt)
      return
    }
    loadMoreView.loadMore()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})